//  Inside Microsoft Exchange 2007 Web Services
//  Appendix B
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;

namespace CreateUserAvailabilityWithTimeZoneLookup
{
    class Program
    {
        static void Main(string[] args)
        {
            // TODO: In order for you to run the CreateGetUserAvailabilityWithTimeZoneLookup method you
            // must have a valid email address to look up availability information for as well as the
            // registry key name of a timezone to use for defining the serialized time zone. Until you
            // provide that information, this main method will not do anything useful.
        }

        /// <summary>
        /// Creates a GetUserAvailabilityRequest instance for an organizer 
        /// using the time zone based on the registry keyname provided. (Listing B-21)
        /// </summary>
        /// <param name="organizer">Email address of the organizer</param>
        /// <returns>GetUserAvailabilityRequest instance.</returns>
        /// 
        public static GetUserAvailabilityRequestType
            CreateGetUserAvailabilityWithTimeZoneLookup(
                EmailAddress organizer,
                string timeZoneRegistryKeyName)
        {
            // Create a SerializableTimeZone for the time zone defined by
            // the passed in time zone key name
            //
            SerializableTimeZone timeZone =
                TimeZoneHelpers.CreateSerializableTimeZoneFromRegistryInformation(
                    timeZoneRegistryKeyName);

            // Create the Mailbox array for all attendees
            //
            MailboxData[] mailboxes = new MailboxData[1];
            mailboxes[0] = new MailboxData();
            mailboxes[0].Email = organizer;
            mailboxes[0].AttendeeType = MeetingAttendeeType.Organizer;

            // Configure the 24-hour window and details for Free/Busy 
            // information.
            //
            Duration duration = new Duration();
            duration.StartTime = new DateTime(
                DateTime.Now.Year,
                DateTime.Now.Month,
                DateTime.Now.Day,
                0,
                0,
                0,
                DateTimeKind.Unspecified);
            duration.EndTime = duration.StartTime.AddDays(1);

            FreeBusyViewOptionsType freeBusyViewOpts =
                new FreeBusyViewOptionsType();
            freeBusyViewOpts.TimeWindow = duration;
            freeBusyViewOpts.RequestedView = FreeBusyViewType.DetailedMerged;
            freeBusyViewOpts.RequestedViewSpecified = true;
            freeBusyViewOpts.MergedFreeBusyIntervalInMinutes = 30;
            freeBusyViewOpts.MergedFreeBusyIntervalInMinutesSpecified = true;

            // Prepare the request
            //
            GetUserAvailabilityRequestType getUserAvailRequest =
                new GetUserAvailabilityRequestType();
            getUserAvailRequest.TimeZone = timeZone;
            getUserAvailRequest.FreeBusyViewOptions = freeBusyViewOpts;
            getUserAvailRequest.MailboxDataArray = mailboxes;

            return getUserAvailRequest;
        }

    }
}
